package com.team.hiring.common.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.team.hiring.common.Constants;
import com.team.hiring.common.dao.CommonDao;
import com.team.hiring.common.util.FastjsonUtil;
import com.team.hiring.common.util.MailUtil;
import com.team.hiring.talent.entity.ResumeEntity;
import com.team.hiring.talent.entity.TalentEntity;

/**
 * Servlet implementation class CommonServlet
 */
public class CommonServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	CommonDao commonDao = new CommonDao();
	public CommonServlet() {
		super();

	}

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding(Constants.ENCODE_UTF8);
		String command = request.getParameter("command");
		if ("toForgetPasswordUI".equals(command)) {
			toForgetPasswordUI(request, response);
		} else if ("sendEmail".equals(command)) {
			sendEmail(request, response);
		}else if ("toUpdatePasswordUI".equals(command)) {
			toUpdatePasswordUI(request, response);
		}else if ("updatePassword".equals(command)) {
			updatePassword(request, response);
		}else if ("loadingData".equals(command)) {
			loadingData(request, response);
		}
	}

	
	/**
	 * 加载一批数据
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private class Info {
		private String companyName;
		private String slogan;
		private String positionName;
		private int salary1;
		private int salary2;
		private String address;

		public Info() {
			super();
		}

		public Info(String companyName, String slogan, String positionName,
				int salary1, int salary2, String address) {
			super();
			this.companyName = companyName;
			this.slogan = slogan;
			this.positionName = positionName;
			this.salary1 = salary1;
			this.salary2 = salary2;
			this.address = address;
		}

		public String getCompanyName() {
			return companyName;
		}

		public void setCompanyName(String companyName) {
			this.companyName = companyName;
		}

		public String getSlogan() {
			return slogan;
		}

		public void setSlogan(String slogan) {
			this.slogan = slogan;
		}

		public String getPositionName() {
			return positionName;
		}

		public void setPositionName(String positionName) {
			this.positionName = positionName;
		}

		public int getSalary1() {
			return salary1;
		}

		public void setSalary1(int salary1) {
			this.salary1 = salary1;
		}

		public int getSalary2() {
			return salary2;
		}

		public void setSalary2(int salary2) {
			this.salary2 = salary2;
		}

		public String getAddress() {
			return address;
		}

		public void setAddress(String address) {
			this.address = address;
		}

	}

	private void loadingData(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		List<Info> list = new ArrayList<>();
		for (int i = 0; i < 10; i++) {
			Info info = new Info("上善云图" + i, "没有口号" + i, "Java开发工程师" + i, 1,
					1000, "重庆" + i);
			list.add(info);
		}

		// 把文本打印给页面，但要确定编码，访问中文乱码
		response.setContentType("text/html; charset=UTF-8");
		response.getWriter().print(FastjsonUtil.toJson(list));
	}
	


	/**
	 * 转到找回密码界面
	 * 
	 * @param request
	 * @param response
	 */
	private void toForgetPasswordUI(HttpServletRequest request,
			HttpServletResponse response) {
		try {
			request.getRequestDispatcher(
					"/WEB-INF/view/common/forget-password.jsp").forward(
					request, response);
		} catch (ServletException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * 发送密码重置邮件
	 * 
	 * @param request
	 * @param response
	 * @throws IOException 
	 * @throws ServletException 
	 */
	private void sendEmail(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String email = request.getParameter("email");
		String vaildateCode = request.getParameter("validateCode");
		String sessionValidateCode = (String) request.getSession()
				.getAttribute("rand");
		String accountType = request.getParameter("accountType");
		// 1.验证码判断
		if (vaildateCode.equals(sessionValidateCode)) {
			// 2.如果验证正确，在执行邮件的发送
			StringBuffer sb = new StringBuffer();
			sb.append("<p>你好 <a href=\"mailto:" + email
					+ "\" target=\"_blank\">" + email + "</a>!</p>");
			sb.append("<p>您已经请求了重置密码，可以点击下面的链接来重置密码</p>");
			sb.append("<p><a href=\"http://localhost:8080/hiring/CommonServlet?command=toUpdatePasswordUI&email="
					+ email
					+ "&accountType="
					+ accountType
					+ "\" target=\"_blank\">http://localhost:8080/hiring/CommonServlet?command=toUpdatePasswordUI&email="
					+ email + "&accountType=" + accountType + "</a></p>");
			sb.append("<p>如果你没有请求重置密码，请忽略这封邮件</p>");
			sb.append("<p>在你点击上面链接修改密码之前，你的密码将会保持不变。</p>");
			// 2、如果验证码正确，再执行邮件的发送
			MailUtil.send(email, "Hiring重置密码信息", sb.toString());
			request.getRequestDispatcher(
					"/WEB-INF/view/company/companylogin.jsp?sentEmail=true").forward(
					request, response);
		} else {
			request.setAttribute("email", email);
			toForgetPasswordUI(request, response);
		}
	}
	
	
	/**
	 * 转到修改密码界面
	 * @param request
	 * @param response
	 */
	private void toUpdatePasswordUI(HttpServletRequest request,
			HttpServletResponse response) {
		try {
			request.getRequestDispatcher(
					"/WEB-INF/view/common/update-password.jsp").forward(
					request, response);
		} catch (ServletException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

	
	
	
	/**
	 * 修改密码
	 * @param request
	 * @param response
	 */
	private void updatePassword(HttpServletRequest request,
			HttpServletResponse response) {
		String email = request.getParameter("email");
		String newpassword = request.getParameter("newPassword");
		String accountType = request.getParameter("accountType");
		boolean result = commonDao.updatePassword(email,newpassword,accountType);
		if(result){
			try {
				request.getRequestDispatcher(
						"/WEB-INF/view/company/companylogin.jsp").forward(
						request, response);
			} catch (ServletException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}else{
			System.err.println("数据修改失败！");
		}
	}
	
	
	
	
	
	
}
